package com.example.fw;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WebDriverHelper {
	
	protected WebDriver driver;
	private final ApplicationManager manager;

	public WebDriverHelper(ApplicationManager manager) {
		this.manager = manager;
		String browser = browser();
		if (driver == null) {
			if ("firefox".equals(browser)) {
				driver = new FirefoxDriver();
			} else if ("ie".equals(browser)) {
				driver = new InternetExplorerDriver();
			} else {
				throw new Error("Unsupported browser: " + browser);
			}

			String baseUrl = manager.getProperty("baseUrl");
			driver.manage()
					.timeouts()
					.implicitlyWait(
							Integer.parseInt(manager
									.getProperty("implicitlyWait")),
							TimeUnit.SECONDS);
			driver.get(baseUrl);
		}
	}

	public void stop() {
		driver.quit();
	}

	public WebDriver getDriver() {
		return driver;
	}

	private String browser() {
		return manager.getProperty("browser", "firefox");
	}

}
